package ex55.mm.flow.impound.quote.common.kit;

import ex55.mm.flow.impound.quote.exchange.api.ExchangeException;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.IOException;

public class ExceptionKit {

    public static ExchangeException handler(Throwable exception) {
        Throwable e = exception;
        Throwable cause = ExceptionUtils.getRootCause(exception);
        if (cause != null) {
            e = cause;
        }

        if (e instanceof IOException) {
            return ExchangeException.newRejectException(e.getMessage(), e);
        }

        if (e instanceof IllegalArgumentException) {
            return ExchangeException.newRejectException(e.getMessage(), e);
        }

        return ExchangeException.newUncertainException(e.getMessage(), e);
    }
}
